package com.tuxing.draa_data.service.oneDb;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.tuxing.draa_data.dao.db.*;
import com.tuxing.draa_data.dao.center.*;
import com.tuxing.draa_data.pojo.db.*;
import com.tuxing.draa_data.pojo.center.*;
import com.tuxing.draa_data.dao.db.Elsevier20220606Dao;
import com.tuxing.draa_data.pojo.db.Crc20220606;
import com.tuxing.draa_data.pojo.db.Elsevier20220606;
import com.tuxing.draa_data.service.BaseService;
import com.tuxing.draa_data.util.ConvertUtil;
import com.tuxing.draa_data.util.FormatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuanshihang
 * @version 1.0
 * @className DatabaseInfoServiceImpl
 * @description 基础描述:数据库期刊图书信息解析实现
 * @date 2022/6/28 16:04
 */
@Service
@Slf4j
public class DatabaseInfoServiceImpl extends BaseService implements DatabaseInfoService {

    @Resource
    private NewDcBibliographyBookDao newDcBibliographyBookDao;

    @Resource
    private NewDcBibliographyBookDetailDao newDcBibliographyBookDetailDao;

    @Resource
    private NewDcBibliographyPeriDao newDcBibliographyPeriDao;

    @Resource
    private NewDcBibliographyPeriDetailDao newDcBibliographyPeriDetailDao;

    @Resource
    private Crc20220606Dao crc20220606Dao;

    @Resource
    private Elsevier20220606Dao elsevier20220606Dao;

    @Resource
    private Weipu20220629Dao weipu20220629Dao;

    @Resource
    private Begell20220606Dao begell20220606Dao;

    @Resource
    private Iet20220601Dao iet20220601Dao;

    @Resource
    private Iop20220607Dao iop20220607Dao;

    @Resource
    private Rsc20220602Dao rsc20220602Dao;

    @Resource
    private Aiaa20220601Dao aiaa20220601Dao;

    @Resource
    private Adis20220630Dao adis20220630Dao;

    @Resource
    private Adis20220630001Dao adis20220630001Dao;

    @Resource
    private Art20220701Dao art20220701Dao;

    @Resource
    private Bsc20220701Dao bsc20220701Dao;

    @Resource
    private Bsp20220701Dao bsp20220701Dao;

    @Resource
    private Bsu20220701Dao bsu20220701Dao;

    @Resource
    private Cinahk20220701Dao cinahk20220701Dao;

    @Resource
    private Embase20220704Dao embase20220704Dao;

    @Resource
    private NewDcBookDao newDcBookDao;

    @Resource
    private NewDcPeriDao newDcPeriDao;

    //region CRC电子图书数据库解析

    /**
     * @descpription 解析crc电子图书信息
     * @author yuanshihang
     * @date 2022/6/28 16:05
     * @Version 1.0
     */
    @Override
    public void parseAndSaveCrcEBook() {
        int maxId = 6000;
        while (true) {
            log.error("当前执行的最大ID为：" + maxId);
            QueryWrapper<Crc20220606> queryWrapper = new QueryWrapper<>();
            queryWrapper.gt("id", maxId);
            queryWrapper.last("order by id asc limit 1000");
            List<Crc20220606> pageList = crc20220606Dao.selectList(queryWrapper);
            if (CollectionUtils.isEmpty(pageList)) {
                return;
            }
            for (Crc20220606 crc20220606 : pageList) {
                try {
                    Long recordId = findBook(crc20220606.getName(), crc20220606.getEisbn(), null, 2);
                    if (recordId == null) {
                        recordId = saveNewBook(crc20220606);
                    }
                    NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1418103942447104L, 1467418103942447104L, 2022);
                    if (oneByBidDidPidYear == null) {
                        NewDcBook dcBook = new NewDcBook();
                        dcBook.createByCrc(1418103942447104L, 1467418103942447104L, recordId, 2022, crc20220606.getUrl(), 2);
                        newDcBookDao.insert(dcBook);
                    }
                } catch (Exception ex) {
                    log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
                }
            }
            if (pageList.size() < 1000) {
                return;
            } else {
                List<Integer> idList = pageList.stream().map(Crc20220606::getId).collect(Collectors.toList());
                maxId = Collections.max(idList);
            }
        }
    }

    /**
     * @param crc crc数据信息
     * @return 图书元数据ID
     * @descpription 保存crc图书信息
     * @author yuanshihang
     * @date 2022/6/28 17:38
     * @Version 1.0
     */
    private Long saveNewBook(Crc20220606 crc) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByCrc(crc);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByCrc(recordId, crc);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    //endregion

    //region elsevier电子图书数据库解析

    /**
     * @descpription 解析elsevier电子图书信息
     * @author yuanshihang
     * @date 2022/6/29 14:57
     * @Version 1.0
     */
    @Override
    public void parseAndSaveElsevierEBook() {
        QueryWrapper<Elsevier20220606> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("PISSN");
        List<Elsevier20220606> elsevierDataList = elsevier20220606Dao.selectList(queryWrapper);
        for (Elsevier20220606 elsevierData : elsevierDataList) {
            try {
                Long recordId = findBook(elsevierData.getName(), elsevierData.getIsbn(), null, 2);
                if (recordId == null) {
                    recordId = saveBookByElsevier(elsevierData);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399812665143298L, 1468428292124137323L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    dcBook.createByCrc(1466399812665143298L, 1468428292124137323L, recordId, 2022, elsevierData.getUrl(), 2);
                    newDcBookDao.insert(dcBook);
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }

        }
    }

    /**
     * @param elsevierData elsevier图书信息
     * @return 图书的ID
     * @descpription 保存elsevier图书信息
     * @author yuanshihang
     * @date 2022/6/29 15:03
     * @Version 1.0
     */
    private Long saveBookByElsevier(Elsevier20220606 elsevierData) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByElsevier(elsevierData);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByElsevier(recordId, elsevierData);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    //endregion

    /**
     * @description 清洗维普中文期刊数据  共15282条
     * @author hanxiang
     * @date 2022/6/29 15:32:51
     * @Version 1.0
     */
    @Override
    public void parseWeipuPeri() {
        List<Weipu20220629> pageList = weipu20220629Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Weipu20220629 weipu20220629 : pageList) {
            try {
                Long recordId = findPeri(weipu20220629.getName(), weipu20220629.getIssn(), weipu20220629.getCn(), 3);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveNewPeri(weipu20220629);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399815248834563L, 1509459078363656198L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByWeiPu(weipu20220629, 2022, recordId);
                    log.info("新增期刊数据：" + dcPeri);
                    newDcPeriDao.insert(dcPeri);
                }
                if (null != recordId) {
                    NewDcBibliographyPeriDetail havePeri = newDcBibliographyPeriDetailDao.selectById(recordId);
                    if (null == havePeri) {
                        NewDcBibliographyPeriDetail periDetail = new NewDcBibliographyPeriDetail();
                        periDetail.createByWeipu(recordId, weipu20220629);
                        newDcBibliographyPeriDetailDao.insert(periDetail);
                    } else {
                        havePeri.setRecordId(recordId);
                        havePeri.setCoverUrl(weipu20220629.getUrl());
                        newDcBibliographyPeriDetailDao.updateById(havePeri);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex, weipu20220629);
            }
        }
    }

    private Long saveNewPeri(Weipu20220629 weipu20220629) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByWeipu(weipu20220629);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.createByWeipu(recordId, weipu20220629);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description Begell Digital Library 图书数据
     * @author hanxiang
     * @date 2022/6/30 10:09:27
     * @Version 1.0
     */
    @Override
    public void parseBegellBook() {
        List<Begell20220606> pageList = begell20220606Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }

        for (Begell20220606 begell20220606 : pageList) {
            begell20220606.setPIsbn(FormatUtil.formatISBN(begell20220606.getPIsbn()));
            begell20220606.setEIsbn(FormatUtil.formatISBN(begell20220606.getEIsbn()));
            try {
                Long recordId = findBook(begell20220606.getName(), begell20220606.getPIsbn(), begell20220606.getEIsbn(), 2);
                if (recordId == null) {
                    recordId = saveBegellNewBook(begell20220606);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399814909095940L, 1468429128731152386L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    dcBook.createByBegell(1466399814909095940L, 1468429128731152386L, recordId, 2022, 2, begell20220606.getUrl());
                    newDcBookDao.insert(dcBook);
                }
                if (null != recordId) {
                    NewDcBibliographyBookDetail haveBook = newDcBibliographyBookDetailDao.selectById(recordId);
                    if (null == haveBook) {
                        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
                        detail.createByBegell(recordId, begell20220606);
                        newDcBibliographyBookDetailDao.insert(detail);
                        log.info("新增图书数据：" + detail);
                    } else {
                        haveBook.setRecordId(recordId);
                        haveBook.setCoverUrl(begell20220606.getCover());
                        newDcBibliographyBookDetailDao.updateById(haveBook);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }

    }

    private Long saveBegellNewBook(Begell20220606 begell20220606) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByBegell(begell20220606);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByBegell(recordId, begell20220606);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    /**
     * @description IET电子书 图书数据
     * @author hanxiang
     * @date 2022/6/30 16:00:33
     * @Version 1.0
     */
    @Override
    public void parseIetBook() {
        List<Iet20220601> pageList = iet20220601Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Iet20220601 iet20220601 : pageList) {
            iet20220601.setPIsbn(FormatUtil.formatISBN(iet20220601.getPIsbn()));
            iet20220601.setEIsbn(FormatUtil.formatISBN(iet20220601.getEIsbn()));
            try {
                Long recordId = findBook(iet20220601.getName(), iet20220601.getPIsbn(), iet20220601.getEIsbn(), 2);
                if (recordId == null) {
                    recordId = saveIetNewBook(iet20220601);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399812858081286L, 1467805887406764034L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    log.info("新增图书数据：" + dcBook);
                    dcBook.createByIet(1466399812858081286L, 1467805887406764034L, recordId, 2022, 2, iet20220601.getUrl());
                    newDcBookDao.insert(dcBook);
                }
                if (null != recordId) {
                    NewDcBibliographyBookDetail haveBook = newDcBibliographyBookDetailDao.selectById(recordId);
                    if (null == haveBook) {
                        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
                        detail.createByIet(recordId, iet20220601);
                        newDcBibliographyBookDetailDao.insert(detail);
                        log.info("新增图书数据：" + detail);
                    } else {
                        haveBook.setRecordId(recordId);
                        haveBook.setCoverUrl(iet20220601.getCover());
                        newDcBibliographyBookDetailDao.updateById(haveBook);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }

        }

    }

    private Long saveIetNewBook(Iet20220601 iet20220601) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByIet(iet20220601);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByIet(recordId, iet20220601);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    /**
     * @description IOP ebooks 图书数据
     * @author hanxiang
     * @date 2022/6/30 16:00:48
     * @Version 1.0
     */
    @Override
    public void parseIopBook() {
        List<Iop20220607> pageList = iop20220607Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }

        for (Iop20220607 iop20220607 : pageList) {
            String pisbn = iop20220607.getPisbn();
            if (StringUtils.isNotBlank(pisbn)) {
                if (pisbn.startsWith("Print ISBN:")) {
                    pisbn = pisbn.replace("Print ISBN:", "");
                }
            }
            iop20220607.setPisbn(FormatUtil.formatISBN(pisbn));
            iop20220607.setEisbn(FormatUtil.formatISBN(iop20220607.getEisbn()));
            try {
                Long recordId = findBook(iop20220607.getName(), iop20220607.getPisbn(), iop20220607.getEisbn(), 2);
                if (recordId == null) {
                    recordId = saveIopNewBook(iop20220607);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399812858081282L, 1467800772494979077L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    dcBook.createByIop(1466399812858081282L, 1467800772494979077L, recordId, 2022, 2, iop20220607.getUrl());
                    log.info("新增图书数据：" + dcBook);
                    newDcBookDao.insert(dcBook);
                }
                if (null != recordId) {
                    NewDcBibliographyBookDetail haveBook = newDcBibliographyBookDetailDao.selectById(recordId);
                    if (null == haveBook) {
                        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
                        detail.createByIop(recordId, iop20220607);
                        newDcBibliographyBookDetailDao.insert(detail);
                        log.info("新增图书数据：" + detail);
                    } else {
                        haveBook.setRecordId(recordId);
                        haveBook.setCoverUrl(iop20220607.getCover());
                        newDcBibliographyBookDetailDao.updateById(haveBook);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }

        }

    }

    private Long saveIopNewBook(Iop20220607 iop20220607) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByIop(iop20220607);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByIop(recordId, iop20220607);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    /**
     * @description RSC eBooks Collection 图书数据
     * @author hanxiang
     * @date 2022/6/30 17:11:39
     * @Version 1.0
     */
    @Override
    public void parseRscBook() {
        List<Rsc20220602> pageList = rsc20220602Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Rsc20220602 rsc20220602 : pageList) {
            rsc20220602.setPIsbn(FormatUtil.formatISBN(rsc20220602.getPIsbn()));
            rsc20220602.setEIsbn(FormatUtil.formatISBN(rsc20220602.getEIsbn()));
            try {
                Long recordId = findBook(rsc20220602.getName(), rsc20220602.getPIsbn(), rsc20220602.getEIsbn(), 2);
                if (recordId == null) {
                    recordId = saveRscNewBook(rsc20220602);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399815248834564L, 1534881876774948868L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    dcBook.createByRsc(1466399815248834564L, 1534881876774948868L, recordId, 2022, 2, rsc20220602.getUrl());
                    log.info("新增图书数据：" + dcBook);
                    newDcBookDao.insert(dcBook);
                }
                if (null != recordId) {
                    NewDcBibliographyBookDetail haveBook = newDcBibliographyBookDetailDao.selectById(recordId);
                    if (null == haveBook) {
                        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
                        detail.createByRsc(recordId, rsc20220602);
                        newDcBibliographyBookDetailDao.insert(detail);
                        log.info("新增图书数据：" + detail);
                    } else {
                        haveBook.setRecordId(recordId);
                        haveBook.setCoverUrl(rsc20220602.getCover());
                        newDcBibliographyBookDetailDao.updateById(haveBook);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }

    }

    private Long saveRscNewBook(Rsc20220602 rsc20220602) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByRsc(rsc20220602);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByRsc(recordId, rsc20220602);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }

    /**
     * @description aiaa 图书数据
     * @author hanxiang
     * @date 2022/6/30 17:37:24
     * @Version 1.0
     */
    @Override
    public void parseAiaaBook() {
        List<Aiaa20220601> pageList = aiaa20220601Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Aiaa20220601 aiaa20220601 : pageList) {
            String pisbn = aiaa20220601.getPisbn();
            if (StringUtils.isNotBlank(pisbn)) {
                if (pisbn.startsWith("ISBN: ")) {
                    pisbn = pisbn.replace("ISBN: ", "");
                }
            }

            String eisbn = aiaa20220601.getEisbn();
            if (StringUtils.isNotBlank(eisbn)) {
                if (eisbn.startsWith("eISBN: ")) {
                    eisbn = eisbn.replace("eISBN: ", "");
                }
            }
            aiaa20220601.setPisbn(FormatUtil.formatISBN(pisbn));
            aiaa20220601.setEisbn(FormatUtil.formatISBN(eisbn));
            try {
                Long recordId = findBook(aiaa20220601.getName(), aiaa20220601.getPisbn(), aiaa20220601.getEisbn(), 2);
                if (recordId == null) {
                    recordId = saveAiaaNewBook(aiaa20220601);
                }
                NewDcBook oneByBidDidPidYear = newDcBookDao.findOneByBidDidPidYear(recordId, 1466399815248834565L, 1534881876774948869L, 2022);
                if (oneByBidDidPidYear == null) {
                    NewDcBook dcBook = new NewDcBook();
                    dcBook.createByAiaa(1466399815248834565L, 1534881876774948869L, recordId, 2022, 2, aiaa20220601.getUrl());
                    log.info("新增图书数据：" + dcBook);
                    newDcBookDao.insert(dcBook);
                }
                if (null != recordId) {
                    NewDcBibliographyBookDetail haveBook = newDcBibliographyBookDetailDao.selectById(recordId);
                    if (null == haveBook) {
                        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
                        detail.createByAiaa(recordId, aiaa20220601);
                        newDcBibliographyBookDetailDao.insert(detail);
                        log.info("新增图书数据：" + detail);
                    } else {
                        haveBook.setRecordId(recordId);
                        haveBook.setCoverUrl(aiaa20220601.getCover());
                        newDcBibliographyBookDetailDao.updateById(haveBook);
                    }
                }
            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }

    }

    private Long saveAiaaNewBook(Aiaa20220601 aiaa20220601) {
        NewDcBibliographyBook book = new NewDcBibliographyBook();
        book.createByAiaa(aiaa20220601);
        newDcBibliographyBookDao.insert(book);
        Long recordId = book.getRecordId();

        NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
        detail.createByAiaa(recordId, aiaa20220601);
        newDcBibliographyBookDetailDao.insert(detail);
        log.info("新增图书数据：" + book);
        return recordId;
    }


    /**
     * @description Adis期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:38:49
     * @Version 1.0
     */
    @Override
    public void parseAdisPeri() {
        List<Adis20220630> pageList = adis20220630Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Adis20220630 adis20220630 : pageList) {
            try {
                Long recordId = findUSPeri(adis20220630.getTitle(), adis20220630.getPrintIssn(), adis20220630.getElectrionicIssn(), 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveAdisPeri(adis20220630);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399814976204806L, 1534881876774948870L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByAdis(adis20220630, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }
                if (null != recordId) {
                    NewDcBibliographyPeriDetail havePeri = newDcBibliographyPeriDetailDao.selectById(recordId);
                    if (null == havePeri) {
                        NewDcBibliographyPeriDetail periDetail = new NewDcBibliographyPeriDetail();
                        periDetail.createByAdis(recordId, adis20220630);
                        newDcBibliographyPeriDetailDao.insert(periDetail);
                    } else {
                        havePeri.setRecordId(recordId);
                        havePeri.setCoverUrl(adis20220630.getImg());
                        newDcBibliographyPeriDetailDao.updateById(havePeri);
                    }
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveAdisPeri(Adis20220630 adis20220630) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByAdis(adis20220630);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.createByAdis(recordId, adis20220630);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description Adis期刊数据2
     * @author hanxiang
     * @date 2022/7/6 9:38:49
     * @Version 1.0
     */
    @Override
    public void parseAdis2Peri() {
        List<Adis20220630001> pageList = adis20220630001Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Adis20220630001 adis20220630001 : pageList) {
            try {
                Long recordId = findUSPeri(adis20220630001.getTitle(), adis20220630001.getISSNprint(), adis20220630001.getISSNelectronic(), 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveAdis2Peri(adis20220630001);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399814976204806L, 1534881876774948870L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByAdis2(adis20220630001, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                } else {
                    oneByBidDidPidYear.setScopeBegin(adis20220630001.getFirstYear());
                    oneByBidDidPidYear.setScopeEnd(adis20220630001.getLastYear());
                    oneByBidDidPidYear.setTimeScope(StringUtils.join(adis20220630001.getFirstYear(), "-", adis20220630001.getLastYear()));
                    newDcPeriDao.updateById(oneByBidDidPidYear);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveAdis2Peri(Adis20220630001 adis20220630001) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByAdis2(adis20220630001);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.createByAdis2(recordId, adis20220630001);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description Art 期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:39:26
     * @Version 1.0
     */
    @Override
    public void parseArtPeri() {
        List<Art20220701> pageList = art20220701Dao.findAllPeri();

        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Art20220701 art20220701 : pageList) {

            try {
                if (null == art20220701.getIndexingandAbstractingStart()) {
                    break;
                }
                Long recordId = findUSPeri(art20220701.getPublicationName(), art20220701.getIssn(), null, 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveArtPeri(art20220701);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399815177531400L, 1534881876774948871L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByArt(art20220701, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveArtPeri(Art20220701 art20220701) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByArt(art20220701);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.create(recordId);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description Business Source Complete期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:40:34
     * @Version 1.0
     */
    @Override
    public void parseBscPeri() {
        List<Bsc20220701> pageList = bsc20220701Dao.findAllPeri();
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Bsc20220701 bsc20220701 : pageList) {
            try {
                if (null == bsc20220701.getIndexingandAbstractingStart()) {
                    break;
                }
                Long recordId = findUSPeri(bsc20220701.getPublicationName(), bsc20220701.getIssn(), null, 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveBscPeri(bsc20220701);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399815177531396L, 1534430380178513927L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByBsc(bsc20220701, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveBscPeri(Bsc20220701 bsc20220701) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByBsc(bsc20220701);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.create(recordId);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }


    /**
     * @description Business Source Premier 期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:41:39
     * @Version 1.0
     */
    @Override
    public void parseBspPeri() {
        List<Bsp20220701> pageList = bsp20220701Dao.findAllPeri();
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Bsp20220701 bsp20220701 : pageList) {
            try {
                if (null == bsp20220701.getIndexingandAbstractingStart()) {
                    break;
                }
                Long recordId = findUSPeri(bsp20220701.getPublicationName(), bsp20220701.getIssn(), null, 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveBspPeri(bsp20220701);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399815177531397L, 1534430380430172162L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByBsp(bsp20220701, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveBspPeri(Bsp20220701 bsp20220701) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByBsp(bsp20220701);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.create(recordId);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description Business Source Ultimate 期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:42:07
     * @Version 1.0
     */
    @Override
    public void parseBsuPeri() {
        List<Bsu20220701> pageList = bsu20220701Dao.findAllPeri();
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Bsu20220701 bsu20220701 : pageList) {
            try {
                if (null == bsu20220701.getIndexingandAbstractingStart()) {
                    break;
                }
                Long recordId = findUSPeri(bsu20220701.getPublicationName(), bsu20220701.getIssn(), null, 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveBsuPeri(bsu20220701);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399814640660486L, 1534430382627987462L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByBsu(bsu20220701, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveBsuPeri(Bsu20220701 bsu20220701) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByBsu(bsu20220701);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.create(recordId);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description CINAHL Plus with Full Text期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:42:23
     * @Version 1.0
     */
    @Override
    public void parseCinahkPeri() {
        List<Cinahk20220701> pageList = cinahk20220701Dao.findAllPeri();
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Cinahk20220701 cinahk20220701 : pageList) {
            try {
                if (null == cinahk20220701.getIndexingandAbstractingStart()) {
                    continue;
                }
                Long recordId = findUSPeri(cinahk20220701.getPublicationName(), cinahk20220701.getIssn(), null, 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveCinahkPeri(cinahk20220701);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399814573551620L, 1468441523532808194L, 2022);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByCinahk(cinahk20220701, 2022, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    private Long saveCinahkPeri(Cinahk20220701 cinahk20220701) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByCinahk(cinahk20220701);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();

        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.create(recordId);
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    /**
     * @description ELS-EM-Embase 期刊数据
     * @author hanxiang
     * @date 2022/7/6 9:42:49
     * @Version 1.0
     */
    @Override
    public void parseEmbasePeri() {
        List<Embase20220704> pageList = embase20220704Dao.selectList(null);
        if (CollectionUtils.isEmpty(pageList)) {
            return;
        }
        for (Embase20220704 embase20220704 : pageList) {
            String pissn = embase20220704.getIssn();
            String eissn = embase20220704.getEissn();
            if (StringUtils.isNotBlank(pissn)) {
                embase20220704.setIssn(FormatUtil.formatISSN(pissn));
            }
            if (StringUtils.isNotBlank(eissn)) {
                embase20220704.setEissn(FormatUtil.formatISSN(eissn));
            }

            try {
                Long recordId = findUSPeri(embase20220704.getEmbasejournaltitles(), embase20220704.getIssn(), embase20220704.getEissn(), 1);
                if (null == recordId) {
                    //没有匹配到期刊  需要将这本期刊同步到元数据中去
                    recordId = saveEmbasePeri(embase20220704);
                }
                NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399812728057858L, 1534881876774948875L, 2021);
                if (null == oneByBidDidPidYear) {
                    NewDcPeri dcPeri = new NewDcPeri();
                    dcPeri.createByEmbase(embase20220704, 2021, recordId);
                    newDcPeriDao.insert(dcPeri);
                }

            } catch (Exception ex) {
                log.error("保存数据错误，错误原因：" + ex.getMessage(), ex);
            }
        }
    }

    @Resource
    private ChangxiangStar20220704Dao changxiangStar20220704Dao;

    /**
     * @descpription 解析畅想之星数据库中文图书
     * @author yuanshihang
     * @date 2022/7/6 17:10
     * @Version 1.0
     */
    @Override
    public void changXiangStarCnBook() {
        List<ChangxiangStar20220704> starList = changxiangStar20220704Dao.selectList(null);
        for (ChangxiangStar20220704 star : starList) {
            NewDcBibliographyBook book = new NewDcBibliographyBook();
            book.createByStar(star);
            newDcBibliographyBookDao.insert(book);

            Long recordId = book.getRecordId();
            NewDcBibliographyBookDetail detail = new NewDcBibliographyBookDetail();
            detail.createByStar(recordId, star);

            NewDcBook newDcBook = new NewDcBook();
            newDcBook.createByRsc(1466399815248834566L, 1534881876774948876L, recordId, 2022, 4, star.getUrl());
            newDcBookDao.insert(newDcBook);
        }
    }

    private Long saveEmbasePeri(Embase20220704 embase20220704) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByEmbase(embase20220704);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();
//
//        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
//        detail.create(recordId);
//        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    @Resource
    private WorldScientific20220706Dao worldScientific20220706Dao;

    /**
     * @descpription worldScientific数据
     * @author yuanshihang
     * @date 2022/7/6 20:04
     * @Version 1.0
     */
    @Override
    public void WorldScientific() {
        List<WorldScientific20220706> selectList = worldScientific20220706Dao.selectList(null);
        for (WorldScientific20220706 scientific : selectList) {
            Long recordId = findPeri(scientific.getName(), scientific.getIssn(), scientific.getEIssn(), 1);
            if (recordId == null) {
                recordId = saveJournalByWorldScientific(scientific);
            }
            NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399815248834567L, 1509457776212619272L, 2021);
            if (null == oneByBidDidPidYear) {
                NewDcPeri dcPeri = new NewDcPeri();
                int oa = 0;
                if (!Strings.isNullOrEmpty(scientific.getOaFlag())) {
                    if (scientific.getOaFlag().equals("Open Access")) {
                        oa = 1;
                    }
                }
                dcPeri.createByWorld(oa, 2021, recordId);
                newDcPeriDao.insert(dcPeri);
            }
        }
    }

    @Resource
    private RscJournal20220706Dao rscJournal20220706Dao;

    /**
     * @descpription 解析rsc英国皇家化学学会数据库
     * @author yuanshihang
     * @date 2022/7/6 20:53
     * @Version 1.0
     */
    @Override
    public void rscJournal() {
        QueryWrapper<NewDcBibliographyBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("LANG_CODE", 4);
        List<NewDcBibliographyBook> selectList = newDcBibliographyBookDao.selectList(queryWrapper);
        for (NewDcBibliographyBook book : selectList) {
            String pIsbn = book.getPIsbn();
            if (!Strings.isNullOrEmpty(pIsbn)) {
                if (pIsbn.length() < 13) {
                    System.out.println(1);
                }
                String formatISBN = FormatUtil.formatISBN(pIsbn);
                book.setPIsbn(formatISBN);
                newDcBibliographyBookDao.updateById(book);
            }


        }

//        List<RscJournal20220706> selectList = rscJournal20220706Dao.selectList(null);
//        for (RscJournal20220706 rsc : selectList) {
//            String name = rsc.getName();
//            String substring = null;
//            String notFormat = rsc.getIssn();
//            if (!Strings.isNullOrEmpty(notFormat) && !notFormat.contains("没有")) {
//                String trim = notFormat.replaceAll(" ", "");
//                String[] issns = trim.split("ISSN");
//                if (issns.length > 1) {
//                    substring = issns[1].trim().substring(0, 9);
//                } else {
//                    substring = issns[0].trim().substring(0, 9);
//                }
//            }
//            rsc.setIssn(substring);
//            Long recordId = findPeri(name, substring, null, 1);
//            if (recordId == null) {
//                recordId = saveJournalByRsc(rsc);
//            } else {
//                NewDcBibliographyPeriDetail periDetail = newDcBibliographyPeriDetailDao.selectById(recordId);
//                if (periDetail == null) {
//                    periDetail = new NewDcBibliographyPeriDetail();
//                    periDetail.setRecordId(recordId);
//                    periDetail.setCoverUrl(rsc.getCoverUrl());
//                    newDcBibliographyPeriDetailDao.insert(periDetail);
//                } else {
//                    periDetail.setCoverUrl(rsc.getCoverUrl());
//                    newDcBibliographyPeriDetailDao.updateById(periDetail);
//                }
//            }
//
//            NewDcPeri oneByBidDidPidYear = newDcPeriDao.findOneByBidDidPidYear(recordId, 1466399813789216770L, 1468452797343809540L, rsc.getYear());
//            if (null == oneByBidDidPidYear) {
//                NewDcPeri dcPeri = new NewDcPeri();
//                dcPeri.createByRsc(rsc, rsc.getYear(), recordId);
//                newDcPeriDao.insert(dcPeri);
//            }
//        }
    }

    private Long saveJournalByRsc(RscJournal20220706 rsc) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByRsc(rsc);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();
//
        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.setRecordId(recordId);
        detail.setOa("0");
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

    private Long saveJournalByWorldScientific(WorldScientific20220706 scientific) {
        NewDcBibliographyPeri peri = new NewDcBibliographyPeri();
        peri.createByWorldScientific(scientific);
        newDcBibliographyPeriDao.insert(peri);
        Long recordId = peri.getRecordId();
//
        NewDcBibliographyPeriDetail detail = new NewDcBibliographyPeriDetail();
        detail.setRecordId(recordId);
        String oaFlag = scientific.getOaFlag();
        String oa = "0";
        if (!Strings.isNullOrEmpty(oaFlag)) {

            if (oaFlag.equals("Open Access")) {
                oa = "1";
            }
        }
        detail.setOa(oa);
        detail.setPubYear(scientific.getPubYear().toString());
        newDcBibliographyPeriDetailDao.insert(detail);
        log.info("新增期刊数据：" + peri);
        return recordId;
    }

}
